

public class 整数转罗马数字 {
    
    public static void main(String[] args) {
        
        System.out.println(intToRoman(1));
        
    }

    private static int[] arrNumber = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    private static String[] arrSymbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

    public static String intToRoman(int num) {
        
        //Java StringBuffer：每次都会对 StringBuffer 对象本身进行操作，而不是生成新的对象。
        // 所以如果需要对字符串进行修改推荐使用 StringBuffer。

        StringBuilder builder = new StringBuilder();

        for(int i=0; i < arrNumber.length; i++){

            int number = arrNumber[i];

            String numberStr = arrSymbols[i];

            while (num >= number) {
                
                num -= number;

                builder.append(numberStr);
            }

            if(num <= 0 ){

                break;
            }

        }

        return builder.toString();
    }
}
